package com.fun.feifun.modules.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fun.feifun.base.utils.common.ListUtil;
import com.fun.feifun.modules.business.model.BusinessOrder;
import com.fun.feifun.modules.business.model.BusinessOrderLog;
import com.fun.feifun.modules.business.dao.BusinessOrderLogMapper;
import com.fun.feifun.modules.business.model.dto.OrderLogSupplier;
import com.fun.feifun.modules.business.service.IBusinessOrderLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fun.feifun.modules.business.service.IBusinessOrderService;
import com.fun.feifun.modules.business.service.IBusinessUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 业务订单日志 服务实现类
 * </p>
 *
 * @author wkf
 * @since 2024-06-20
 */
@Service
public class BusinessOrderLogServiceImpl extends ServiceImpl<BusinessOrderLogMapper, BusinessOrderLog> implements IBusinessOrderLogService {

    @Override
    public void saveLog(BusinessOrder order, String operationRemake, Integer index) {
        BusinessOrderLog log=new BusinessOrderLog();
        int no = this.count(new QueryWrapper<BusinessOrderLog>().lambda().eq(BusinessOrderLog::getOrderId,order.getId()));
        log.loadOreder(order,operationRemake,no+1);
        log.setRouteIndex(index);
        this.save(log);
    }
    @Override
    public void saveLog(BusinessOrder order, String operationRemake) {
        BusinessOrderLog log=new BusinessOrderLog();
        int no = this.count(new QueryWrapper<BusinessOrderLog>().lambda().eq(BusinessOrderLog::getOrderId,order.getId()));
        log.loadOreder(order,operationRemake,no+1);
        this.save(log);
    }
    @Override
    public List<BusinessOrderLog> getOrderLog(Long orderId) {
        QueryWrapper<BusinessOrderLog> wrapper=new QueryWrapper<BusinessOrderLog>();
        wrapper.lambda().eq(BusinessOrderLog::getOrderId,orderId).orderByAsc(BusinessOrderLog::getNo);
        return  this.list(wrapper);
    }

    @Override
    public List<OrderLogSupplier> getOrderSupplierIds(List<Long> orderIds) {
        return this.baseMapper.getOrderSupplierIds(orderIds);
    }

    @Override
    public BusinessOrderLog getMaxIndexSupplier(Long orderId) {
        QueryWrapper<BusinessOrderLog> wrapper=new QueryWrapper<BusinessOrderLog>();
        wrapper.lambda().eq(BusinessOrderLog::getOrderId,orderId).orderByDesc(BusinessOrderLog::getRouteIndex);
        List<BusinessOrderLog> list = this.list(wrapper);
        if(ListUtil.isEmpty(list)){
            return new BusinessOrderLog().setRouteIndex(0);
        }
        return this.list(wrapper).get(0);
    }
}
